package com.party.poster.draw;

import com.party.poster.PosterElement;
import com.party.poster.element.Img;
import com.party.poster.enums.EnumShape;

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;

public class ImgDraw implements PosterDraw{

    @Override
    public boolean draw(Graphics2D graphics, PosterElement element) {
        if (element instanceof Img == false){
            return false;
        }
        Img img = (Img) element;

        int w = img.getSize().getWidth();
        int h = img.getSize().getHeight();
        int x = img.getPosition().getX();
        int y = img.getPosition().getY();
        BufferedImage image = img.getImage();

        //如果指定的宽高都大于0,并且高宽不等于图片的宽高就压缩
        if ( (w > 0 && h > 0) &&
                (w != image.getWidth() && h != image.getHeight())){
            image = appointContraction(image,w,h);
        }
        //绘制圆
        if (EnumShape.Circular.equals(img.getShape())){
            drawCircular(graphics,image,x,y,h);
        }if (EnumShape.TyCircular.equals(img.getShape())){
            //绘制椭圆
            drawTyCircular(graphics,image,x,y,w,h);
        }else if (EnumShape.None.equals(img.getShape())){
            graphics.drawImage(image, x,y,image.getWidth(), image.getHeight(), null);
        }
        return true;
    }

    /**
     * 绘制圆图
     * @param graphics
     */
    public void drawCircular(Graphics2D graphics,BufferedImage image,int x,int y,int h){
        int border = 1;
        int diameter = h - border * 2;
        Ellipse2D.Double shapeEll = new Ellipse2D.Double(x,y + border, diameter, diameter);
        // 抗锯齿
        graphics.setBackground(Color.WHITE);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setClip(shapeEll);
        graphics.drawImage(image.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_SMOOTH),
                x,y, null);
    }
    /**
     * 绘制椭圆圆图
     * @param graphics
     */
    public void drawTyCircular(Graphics2D graphics,BufferedImage image,int x,int y,int w,int h){
        int border = 1;
        int diameter = h - border * 2;
        // 1. x：椭圆左上角边界框的x坐标
        // 2. y：椭圆左上角边界框的y坐标
        // 3. w：椭圆边界框的宽度
        // 4. h：椭圆边界框的高度

        Ellipse2D.Double shapeEll = new Ellipse2D.Double(x-1,y + border, 220, 230);
        // 抗锯齿
        graphics.setBackground(Color.WHITE);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setClip(shapeEll);
        graphics.drawImage(image.getScaledInstance(220, 333, Image.SCALE_SMOOTH),
                x,y, null);
    }


    /**
     * 压缩
     * @param image
     * @param w
     * @param h
     * @return
     */
    public BufferedImage appointContraction(BufferedImage image,int w,int h){
        BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = buffImg.createGraphics();
        graphics.setBackground(Color.WHITE);  //置Graphics2D上下文的背景颜色。 背景颜色用于清除区域
        graphics.setColor(Color.WHITE); //将此图形上下文的当前颜色设置为指定的颜色。
        graphics.fillRect(0, 0, w, h); //填写指定的矩形
        graphics.drawImage(image.getScaledInstance(w, h, Image.SCALE_SMOOTH), 0, 0, null);
        graphics.dispose();
        return buffImg;
    }

}
